#########################################################################
# File : /lua/ui/network/lobbycomm.lua
# Description : Dark Skies specific lobbycomm module
# Author : Neruz
#########################################################################

quietTimeout = 10000.0 # milliseconds to wait before booting people
maxPlayerSlots = 36
maxConnections = 39 # count doesn't include ourself.

Strings = {
    # General info strings
    ['Connecting'] = "<LOC lobui_0083>Connecting to Game", 
    ['AbortConnect'] = "<LOC lobui_0204>Abort Connect", 
    ['TryingToConnect'] = "<LOC lobui_0331>Connecting...", 
    ['TimedOut'] = "<LOC lobui_0205>%s timed out.", 
    ['TimedOutToHost'] = "<LOC lobui_0206>Timed out to host.", 
    ['Ejected'] = "<LOC lob_0000>You have been ejected: %s", 
    ['ConnectionFailed'] = "<LOC lob_0001>Connection failed: %s", 
    ['LaunchFailed'] = "<LOC lobui_0207>Launch failed: %s", 
    ['LobbyFull'] = "<LOC lobui_0279>The game lobby is full.", 

    # Error reasons
    ['StartSpots'] = "<LOC lob_0002>The map does not support this number of players.", 
    ['NoConfig'] = "<LOC lob_0003>No valid game configurations found.", 
    ['NoObservers'] = "<LOC lob_0004>Observers not allowed.", 
    ['KickedByHost'] = "<LOC lob_0005>Kicked by host.", 
    ['NoLaunchLimbo'] = "<LOC lob_0006>No clients allowed in limbo at launch", 
    ['HostLeft'] = "<LOC lob_0007>Host abandoned lobby", 
}

function GetDefaultPlayerOptions(playerName)
    return {
        PlayerColor = 1,
        ArmyColor = 1,
        Ready = false,
        Faction = 1,
        PlayerName = playerName or "player",
        Human = true,
        Civilian = false,
    }
end

DiscoveryService = Class(moho.discovery_service_methods) {
    RemoveGame = function(self, index)
                     WARN('LOBBYCOMM', 'DiscoveryService.RemoveGame(' .. tostring(index) ..')')
                 end, 
    GameFound = function(self, index, gameConfig)
                    WARN('LOBBYCOMM', 'DiscoveryService.GameFound(' .. tostring(index) ..')')
                    WARN('LOBBYCOMM', repr(gameConfig))
                end, 
    GameUpdated = function(self, index, gameConfig)
                      WARN('LOBBYCOMM', 'DiscoveryService.GameUpdated(' .. tostring(index) ..')')
                      WARN('LOBBYCOMM', repr(gameConfig))
                  end, 
}

function CreateDiscoveryService()
    local service = InternalCreateDiscoveryService(DiscoveryService)
    WARN('LOBBYCOMM', '*** DISC CREATE:', service)
    return service
end

LobbyComm = Class(moho.lobby_methods) {

    # General events you should override
    Hosting = function(self) end,
    ConnectionFailed = function(self, reason) KillThread(self._timeoutThread) end,
    ConnectionToHostEstablished = function(self,ourID,hostID) KillThread(self._timeoutThread) end,
    Ejected = function(self,reason) end,
    SystemMessage = function(self, text) LOG('System: ' .. text) end,
    DataReceived = function(self, data)  end,
    GameConfigRequested = function(self) end,
    PeerDisconnected = function(self,peerName,uid) end,
    LaunchFailed = function(self,reasonKey) end,

    JoinGame = function(self, addressStr, remotePlayerName, remotePlayerUIDStr)
		self._timeoutThread = ForkThread(function()
			WaitSeconds(quietTimeout / 1000)
			self:ConnectionFailed(LOC(Strings.TimedOutToHost))
		end)
		moho.lobby_methods.JoinGame(self, addressStr, remotePlayerName, remotePlayerUIDStr)
	end,
	
    # native void __init(self, protocol, localport, maxconnections, localPlayerName, localPlayerUID, natTraversalProvider)
    # native void SendData(self, targetID, data)
    # native void BroadcastData(self,data)
    # native void Destroy(self)
    # native bool IsHost(self)
    # native table GetPeers(self)
    # native int GetPlayerCount(self)
    # native string GetLocalPlayerName(self)
    # native void EjectPeer(self, targetID, reason)
    # native string MakeValidGameName(self,desiredName)
    # native string MakeValidPlayerName(self,uid,desiredName)
    # native void HostGame(self)
    # native void JoinGame(self, addressStr, string-or-nil remotePlayerName, remotePlayerUIDStr)
    # native void LaunchGame(self,gameInfo)
    # native void DebugDump(self)
    # native string GetLocalPlayerID(self)
    # native int-or-nil GetLocalPort(self)

    # Used by Impulse
    # native void ConnectToPeer(addressStr,port,name,uidStr)
    # native void DisconnectFromPeer(uidStr)
}

function CreateLobbyComm(protocol, localport, localPlayerName, localPlayerUID, natTraversalProvider)
    return LobbyComm(protocol, localport, maxConnections, localPlayerName, localPlayerUID, natTraversalProvider)
end